﻿using System;
using System.Collections.Generic;
using System.Linq;
using CF.Data.Entity.FrmMemberEntity;
using CF.Member.DAL.WeiboMember;

namespace CF.Member.DAL.Member
{
    public class FM_MemberDAL
    {
        public const int PasswordLimit1 = 5;
        public const int PasswordLimit2 = 16;

        /// <summary>
        /// 检查昵称是否有重复
        /// </summary>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public bool CheckNickName(string nickName)
        {
            bool result = false;
            caifendbEntities context = new caifendbEntities();
            var query = context.FM_Member.Where(c => c.NickName.ToLower() == nickName.ToLower());
            if (query.Count() > 0)
            {
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 检查是否已经存在的微博用户。
        /// </summary>
        /// <param name="bindUid"></param>
        /// <param name="bindType"></param>
        /// <returns></returns>
        public bool CheckIsBinding(string bindUid, string bindType)
        {
            bool result = false;
            caifendbEntities context = new caifendbEntities();
            var query = context.FM_Member.Where(c => c.BindOtherUID == bindUid && c.BindType == bindType);
            if (query.Count() > 0)
            {
                result = true;
            }
            return result;
        }


        //public bool BgLogin(string userName, string password, out int uid)
        //{
        //    bool result = false;
        //    uid = 0;
        //    string newPsw = password + PasswordSalt.AdminSalt;
        //    string hashPsw = FormsAuthentication.HashPasswordForStoringInConfigFile(newPsw, "md5");
        //    //MD5加密密码，然后取6～21个字符
        //    hashPsw = hashPsw.ToLower().Substring(PasswordLimit1, PasswordLimit2);
        //    caifendbEntities context = new caifendbEntities();
        //    var query = context.FM_Member.Where(c => c.MemberID == userName && c.State == MemberState.Administrator);
        //    if (query.Count() > 0)
        //    {
        //        FM_Member member = query.First();
        //        if (!string.IsNullOrWhiteSpace(member.Password) && member.Password.ToLower() == hashPsw)
        //        {
        //            result = true;
        //            uid = member.UID;
        //        }
        //    }
        //    return result;
        //}

        /// <summary>
        /// 记录登录信息，包括连续登录次数、总登录次数、登录时间等。
        /// </summary>
        public void MarkLoginInfo(int uid, string loginIp)
        {
            caifendbEntities context = new caifendbEntities();
            FM_Member member = context.FM_Member.FirstOrDefault(c => c.UID == uid);
            if (member != null)
            {
                DateTime lastLogin = member.LastLogin ?? DateTime.Now;
                int daySpan = CommonCls.DateUtils.DayDifference(lastLogin, DateTime.Now);
                if (daySpan == 1)
                {
                    member.ConcatLoginCount++;
                    member.LoginDaysCount++;
                }
                else if (daySpan > 1)
                {
                    member.ConcatLoginCount = 1;
                    member.LoginDaysCount++;
                }
                member.LoginCount++;
                member.LastLogin = DateTime.Now;
                member.LastLoginIP = loginIp;
                context.SaveChanges();
            }
        }


        public List<FM_Member> GetAllUser(int start, int limit)
        {
            List<FM_Member> memlist = new List<FM_Member>();
            caifendbEntities context = new caifendbEntities();
            context.ContextOptions.LazyLoadingEnabled = false;
            var query = context.FM_Member.Where(c => c.State == MemberState.User).OrderBy(c => c.UID).Skip(start).Take(limit);
            memlist = query.ToList();
            return memlist;
        }

        #region 20120815 czh
        /// <summary>
        /// 检查会员绑定的关系状态
        /// </summary>
        /// <param name="bindType"></param>
        /// <param name="otherName"></param>
        /// <returns></returns>
        public void OutSideMemRelation(string weiboUID, string bindType, out int uid, out int rcode)
        {
            using (caifendbEntities context = new caifendbEntities())
            {

                uid = 0;
                rcode = 0;
                try
                {
                    var query = from items in context.FM_Member where items.BindOtherUID == weiboUID && items.BindType == bindType select items;

                    if (query.Count() > 0)
                    {
                        //有帐号
                        rcode = 1000;
                        uid = query.First().UID;

                    }
                    else
                    {
                        //木有帐号
                        rcode = -1000;

                    }
                }
                catch (Exception ex)
                {
                    rcode = -1001;

                }

            }
        }

        /// <summary>
        /// 成功注册
        /// </summary>
        /// <param name="bindUid"></param>
        /// <param name="bindType"></param>
        /// <param name="nickname"></param>
        /// <param name="sex"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool AddMemberInfo(string bindUid, string bindType, string nickname, string sex, out int uid)
        {
            caifendbEntities context = new caifendbEntities();
            uid = 0;
            FM_Member member = new FM_Member();
            member.NickName = nickname;
            member.Sex = sex;
            member.ULevel = 1;
            member.ConcatLoginCount = 1;
            member.LoginCount = 0;
            member.LoginDaysCount = 1;
            member.TtlAmount = 0;
            member.AmountTotle = 0;
            member.Experience = 0;
            member.State = 0;
            member.LastLogin = DateTime.Now;
            member.BindOtherUID = bindUid;
            member.BindType = bindType;
            member.RegistTime = DateTime.Now;
            context.AddToFM_Member(member);
            int count = context.SaveChanges();
            if (count > 0)
            {
                uid = member.UID;
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion


    }
}
